﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using Frame.Lib.Core.Module;
using Frame.Lib.Utilities;
using Frame.Lib.Common;
using Frame.Lib.Enums;
using Frame.Lib.Core;


public partial class Admin_Module_List : PageList
{
    private ModuleBO Module
    {
        get
        {
            return ModuleBO.Instance<ModuleBO>();
        }
    }
    private string strOrders;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindView();
        }
        AddAttr();
        InitSelectID(hidSelectIDs);       
        strOrders = hidOrders.Value;
        hidOrders.Value = "";

        JSUtil.SetTableHeight("list", 80);       
    }

    protected string GetIcon(object url)
    {
        if (url == null || url.ToString() == "")
        {
            return "";
        }
        else
        {
            string img = "<img src='" + GlobalVar.WebAppPath + url + "' alt='' style='margin-right:5px;' align='absmiddle' />";
            return img;
        }
    }

    private void AddAttr()
    {
        JSUtil.DelConfirm(btnDelete, "模块");
        JSUtil.MoveTree(btnMove, STableName.Module, hidSelectIDs, btnRefresh);

        JSUtil.ClickBtn(lnkDelete, btnDelete);
        JSUtil.ClickBtn(lnkMove, btnMove);
        JSUtil.ClickBtn(lnkSaveOrders, btnSaveOrders);
        JSUtil.ClickBtn(lnkCopy, btnCopy);
        JSUtil.ClickBtn(lnkStartup, btnEnable);
        JSUtil.ClickBtn(lnkStop, btnDisable);
       
    }

    private void BindView()
    {
        LVUtil.Init(lv, lvHeader);
        IEnumerable<ModuleInfo> list = Module.GetTreeList();

       


        lv.DataSource = list;
        lv.DataKeyNames = new string[] { "ID","UpperID" };
        lv.DataBind();


        if (IsPostBack)
        {
            LVUtil.RestoreTreeListState(lv, hidHideTrIDs.Value);
        }

    }
    protected void btnRefresh_Click(object sender, EventArgs e)
    {
        BindView();
    }
    protected void lv_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            LVUtil.SetEdit(sender, e, 320, 251);
            HtmlTableRow row = null;
            foreach (Control ctl in e.Item.Controls)
            {
                if (ctl is HtmlTableRow)
                {
                    row = (HtmlTableRow)ctl;
                    break;
                }
            }
            string strID = lv.DataKeys[((ListViewDataItem)e.Item).DataItemIndex]["ID"].ToString();
            row.Attributes["id"] = strID;
            string upperID = lv.DataKeys[((ListViewDataItem)e.Item).DataItemIndex]["UpperID"].ToString();
            row.Attributes.Add("UpperID", upperID);

            CheckBox chkSelect = (CheckBox)e.Item.FindControl("chkSelect");
            if (upperID == Constants.TREE_ROOT_UPPER_ID.ToString())
            {
                chkSelect.Enabled = false;
            }

            string strUrl = GlobalVar.WebCurrPath + "Edit.aspx?UpperID=" + StrUtil.EncryptUrl(strID);
            HyperLink lnkAdd = (HyperLink)e.Item.FindControl("hypAdd");
            JSUtil.ShowForm(lnkAdd, "增加模块", null, strUrl, 320, 251);
        }
    }
   
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        if (strSelectIDs == "")
        {
            return;
        }

        List<int> ids = StrUtil.SplitToInt(strSelectIDs);

        Module.Delete(ids);
        BindView();
    }

    protected string GetClass(object state)
    {
        if ((int)state == (int)ModuleState.Disable)
        {
            return "disable";
        }
        else
        {
            return "";
        }
    }

    protected void btnCopy_Click(object sender, EventArgs e)
    {
        if (strSelectIDs == "")
        {
            return;
        }

        List<int> ids = StrUtil.SplitToInt(strSelectIDs);

        this.Module.Copy(ids);

        BindView();

    }
   
    protected void btnSaveOrders_Click(object sender, EventArgs e)
    {
        List<int> ids = new List<int>();
        List<int> orders = new List<int>();
        StrUtil.SplitIDOrders(ids, orders, strOrders);
        Module.SaveOrders(ids, orders);
        BindView();     
        
    }

    protected void btnEnable_Click(object sender, EventArgs e)
    {
        if (strSelectIDs == "")
        {
            return;
        }

        this.Module.ChangeState(StrUtil.SplitToInt(strSelectIDs), ModuleState.Enable);
        BindView();
    }

    protected void btnDisable_Click(object sender, EventArgs e)
    {
        if (strSelectIDs == "")
        {
            return;
        }
        this.Module.ChangeState(StrUtil.SplitToInt(strSelectIDs), ModuleState.Disable);
        BindView();

    }
}
